package com.fcy.teamwork.security.environment.checknode

import android.content.Context
import android.net.Proxy
import android.os.Build
import android.text.TextUtils
import com.fcy.teamwork.base.log
import com.fcy.teamwork.security.environment.SecurityCheckNode


class ProxyCheck : SecurityCheckNode {
    /*
 * 判断设备 是否使用代理上网
 * */
    private fun isWifiProxy(context: Context): Boolean {
        // 是否大于等于4.0
        val IS_ICS_OR_LATER = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH
        val proxyAddress: String
        val proxyPort: Int
        if (IS_ICS_OR_LATER) {
            proxyAddress = System.getProperty("http.proxyHost") ?: ""
            val portStr = System.getProperty("http.proxyPort")
            proxyPort = (portStr ?: "-1").toInt()
        } else {
            proxyAddress = Proxy.getHost(context)
            proxyPort = Proxy.getPort(context)
        }
        return !TextUtils.isEmpty(proxyAddress) && proxyPort != -1
    }

    override fun isSecurity(context: Context?): Boolean {
        val res =  !isWifiProxy(context!!)
        if(!res) log("代理检测未通过")
        return res
    }
}